package com.jichangxiu.framework.utils;

import cn.hutool.core.date.DateUtil;
import com.jichangxiu.common.constant.Constants;
import com.jichangxiu.common.constant.RedisConstants;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

public class NoUtils {

    private final static String FORMAT_CODE = "00000";

    public static synchronized String createNo(String tenantId, String type) {
        String redisKey = RedisConstants.createNo(Constants.JCX_PROPERTIES.getProjectName(), tenantId, type, DateUtil.format(new Date(), "yyyyMMdd"));
        Long count = RedisUtils.increment(redisKey, 1);
        RedisUtils.expire(redisKey, 1, TimeUnit.DAYS);
        String code = new DecimalFormat(FORMAT_CODE).format(count);
        return type.toUpperCase() + "_" + DateUtil.format(new Date(), "yyyyMMdd") + "_" + code;
    }

}
